package com.hmall.user.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmall.user.pojo.Address;
import com.hmall.user.service.IAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private IAddressService addressService;

    /**
     *根据addressId查询Address
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Address findAddressById(@PathVariable("id") Long id) {
        return addressService.getById(id);
    }

    /**
     * 根据用户id查询地址列表
     * @param userId
     * @return
     */
    @GetMapping("/uid/{userId}")
    public List<Address> findAddressByUserId(@PathVariable("userId") Long userId) {
        return addressService.query().eq("user_id", userId).list();
    }

    @GetMapping("/default/{userId}")
    public Address defaultAddress(@PathVariable("userId") Long userId) {

        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getIsDefault, 1).eq(Address::getUserId, userId);

        return addressService.getOne(wrapper);
    }
}
